package org.ggn.scoreanalyze.view;

import org.ggn.scoreanalyze.model.ScorePanel;
import org.ggn.scoreanalyze.util.FileUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * Created by guin_guo on 2015/12/9.
 */
public class MainView extends JFrame implements ActionListener {
    //头部
    private JMenuBar menuBar;
    private JMenu menu;
    private JMenuItem menuItem1;
    private JMenuItem menuItem2;
    private JMenuItem menuItem3;
    private JMenuItem menuItem4;
    private JMenuItem menuItem5;

    //搜索框
    private JTextField jtf;
    private JPanel searchPanel;
    private JLabel searchLabel;

    //中间数据框
    ScorePanel scorePanel;
    JPanel centerPanel;//中间
    JPanel centerLeft;//中间左
    JPanel centerRight;//右
    JScrollPane jScrollPane;
    JTable table;
    JLabel figureAnal;//数据分析

    JPanel cr_top, cr_center, cr_bottom;//右边布局板
    JLabel max, min, ave;//三个标签
    JTextField maxJtf, minJtf, aveJtf;//三个显示

    JLabel yx, lh, zd, jg, bjg;//五个标签
    JTextField yxJtf, lhJtf, zdJtf, jgJtf, bjgJtf, pyx, plh, pzd, pjg, pbjg;//中间数据
    JLabel renzhan = new JLabel("人，占");
    JLabel percent = new JLabel("%");

    JButton zhuxing = new JButton("显示柱形分析图");
    JButton bingtu = new JButton("显示饼形分析图");

    String msg;
    JLabel status;//底部状态栏

    //背景图片
    private JPanel imagePanel;
    private ImageIcon background;

    public static void main(String[] args) {

        JFrame.setDefaultLookAndFeelDecorated(true);
        System.setProperty("Quaqua.tabLayoutPolicy", "wrap");
        try {
            UIManager.setLookAndFeel("ch.randelshofer.quaqua.QuaquaLookAndFeel");
        }catch(Exception e){}

        MainView mainView = new MainView();
        mainView.header();
        mainView.init();
        mainView.normal();
    }

    public MainView() {
    }

    /**
     * 整个程序初始化数据
     */
    private void init() {
        //中间数据框
        centerPanel = new JPanel();
        centerLeft = new JPanel(new BorderLayout(0, 0));
        centerRight = new JPanel(new GridLayout(3, 1, 0, 0));

        table = MyTableModel.defaultTable();
        jScrollPane = new JScrollPane(table);
        centerLeft.add(jScrollPane);
        centerLeft.setBorder(BorderFactory.createTitledBorder("成绩单"));
        centerPanel.setBounds(new Rectangle());
        centerPanel.add(centerLeft, BorderLayout.WEST);

        //右边数据头部

        figureAnal = new JLabel("数据分析");
        max = new JLabel("最高分");
        min = new JLabel("最低分");
        ave = new JLabel("平均分");
        maxJtf = new JTextField(3);
        maxJtf.setSize(50, 100);
        minJtf = new JTextField(3);
        minJtf.setSize(50, 100);
        aveJtf = new JTextField(3);
        maxJtf.setSize(50, 100);

        cr_top = new JPanel(null);
        max.setBounds(20, 20, 100, 50);
        cr_top.add(max);
        maxJtf.setBounds(140, 30, 80, 30);
        cr_top.add(maxJtf);
        min.setBounds(20, 50, 100, 50);
        cr_top.add(min);
        minJtf.setBounds(140, 60, 80, 30);
        cr_top.add(minJtf);
        ave.setBounds(20, 80, 100, 50);
        cr_top.add(ave);
        aveJtf.setBounds(140, 90, 80, 30);
        cr_top.add(aveJtf);
        centerRight.add(cr_top);

        //右边数据中间
        yx = new JLabel("优秀(90-100)");
        lh = new JLabel("良好(80-89)");
        zd = new JLabel("中等(70-79)");
        jg = new JLabel("及格(60-69)");
        bjg = new JLabel("不及格(0-60)");
        yxJtf = new JTextField(5);
        pyx = new JTextField(3);
        lhJtf = new JTextField(5);
        plh = new JTextField(3);
        zdJtf = new JTextField(5);
        pzd = new JTextField(3);
        jgJtf = new JTextField(5);
        pjg = new JTextField(3);
        bjgJtf = new JTextField(5);
        pbjg = new JTextField(3);

        //五个分析数据
        GridLayout cr_ctLay = new GridLayout(5, 5, 10, 10);
        cr_center = new JPanel(cr_ctLay);
        cr_center.add(yx);
        cr_center.add(yxJtf);
        cr_center.add(renzhan);
        cr_center.add(pyx);
        cr_center.add(percent);
        renzhan = new JLabel("人，占");
        percent = new JLabel("%");
        cr_center.add(lh);
        cr_center.add(lhJtf);
        cr_center.add(renzhan);
        cr_center.add(plh);
        cr_center.add(percent);
        renzhan = new JLabel("人，占");
        percent = new JLabel("%");
        cr_center.add(zd);
        cr_center.add(zdJtf);
        cr_center.add(renzhan);
        cr_center.add(pzd);
        cr_center.add(percent);
        renzhan = new JLabel("人，占");
        percent = new JLabel("%");
        cr_center.add(jg);
        cr_center.add(jgJtf);
        cr_center.add(renzhan);
        cr_center.add(pjg);
        cr_center.add(percent);
        renzhan = new JLabel("人，占");
        percent = new JLabel("%");
        cr_center.add(bjg);
        cr_center.add(bjgJtf);
        cr_center.add(renzhan);
        cr_center.add(pbjg);
        cr_center.add(percent);
        centerRight.add(cr_center);

        //中间底部
        cr_bottom = new JPanel(null);
        zhuxing.setBounds(20, 65, 150, 50);
        zhuxing.addActionListener(this);
        cr_bottom.add(zhuxing);
        bingtu.setBounds(200, 65, 150, 50);
        cr_bottom.add(bingtu);
        bingtu.addActionListener(this);
        centerRight.add(cr_bottom, BorderLayout.SOUTH);

        centerRight.setBorder(BorderFactory.createTitledBorder("数据分析"));
        centerPanel.add(centerRight, BorderLayout.EAST);

        //底部
        status = new JLabel();
        status.setBorder(BorderFactory.createLineBorder(Color.gray));
        status.setFont(new Font("宋体", Font.BOLD, 14));
        add(centerPanel);
        add(status, BorderLayout.SOUTH);

    }

    /**
     * 程序主要的按钮监听方法
     * @param e
     */
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == menuItem1 || e.getSource() == menuItem3) {
            scorePanel = FileUtils.OpenFile();
            if (scorePanel != null) {
                openFile(scorePanel);
                table.repaint();
                table.updateUI();
            }
        } else if (e.getSource() == menuItem2) {
            /**
             * 另存为文本文件成绩单
             */
            if (scorePanel != null) {
                FileUtils.saveFile(scorePanel, "txt");
            } else {
                FileUtils.showMsg("请打开文件");
            }
        } else if (e.getSource() == menuItem4) {
            /**
             * 另存为文本文件成绩单
             */
            if (scorePanel != null) {
                FileUtils.saveFile(scorePanel, "score");
            } else {
                FileUtils.showMsg("请打开文件");
            }
        } else if (e.getSource() == menuItem5) {
            /**
             * 清除数据
             */
            clear();
            table.repaint();
            table.updateUI();
        } else if (e.getSource() == zhuxing) {
            /**
             * 柱形图
             */
            if (scorePanel != null) {
                BarFrame barFrame = new BarFrame(scorePanel.getClazz() + ":" + scorePanel.getName(),scorePanel.getYx(), scorePanel.getLh(), scorePanel.getZd(), scorePanel.getJg(), scorePanel.getBjg());
            } else {
                FileUtils.showMsg("请打开文件");
            }
        } else if (e.getSource() == bingtu) {
            /**
             * 饼图
             */
            if (scorePanel != null) {
                PieFrame pieFrame = new PieFrame(scorePanel.getClazz() + ":" + scorePanel.getName(),scorePanel.getYx(), scorePanel.getLh(), scorePanel.getZd(), scorePanel.getJg(), scorePanel.getBjg());
            } else {
                FileUtils.showMsg("请打开文件");
            }
        } else {
            FileUtils.showMsg("error");
        }
    }

    /**
     * 清楚程序上的数据
     */
    private void clear() {
        table.setModel(MyTableModel.getTableModel(null));
        maxJtf.setText("");
        minJtf.setText("");
        aveJtf.setText("");
        yxJtf.setText("");
        lhJtf.setText("");
        zdJtf.setText("");
        jgJtf.setText("");
        bjgJtf.setText("");
        pyx.setText("");
        plh.setText("");
        pzd.setText("");
        pjg.setText("");
        pbjg.setText("");
        msg = "";
        status.setText(msg);
    }

    /**
     * 打开文件
     * @param scorePanel
     */
    private void openFile(ScorePanel scorePanel) {
        table.setModel(MyTableModel.getTableModel(scorePanel.getDatas()));
        maxJtf.setText(scorePanel.getHighest() + "");
        minJtf.setText(scorePanel.getLowest() + "");
        aveJtf.setText(String.format("%.2f", scorePanel.getAverage()));
        yxJtf.setText(scorePanel.getYx() + "");
        lhJtf.setText(scorePanel.getLh() + "");
        zdJtf.setText(scorePanel.getZd() + "");
        jgJtf.setText(scorePanel.getJg() + "");
        bjgJtf.setText(scorePanel.getBjg() + "");
        pyx.setText(String.format("%.2f", scorePanel.getPyx()));
        plh.setText(String.format("%.2f", scorePanel.getPlh()));
        pzd.setText(String.format("%.2f", scorePanel.getPzd()));
        pjg.setText(String.format("%.2f", scorePanel.getPjg()));
        pbjg.setText(String.format("%.2f", scorePanel.getPbjg()));
        msg = scorePanel.getFilePath() + "     共" + scorePanel.getStudents().size() + " 人";
        status.setText(msg);
    }

    /**
     * 头部初始化
     */
    private void header() {
        //头部
        menuBar = new JMenuBar();
        menu = new JMenu("文件");
        menuItem1 = new JMenuItem("打开文本文件成绩单");
        menuItem2 = new JMenuItem("另存为文本文件成绩单");
        menuItem3 = new JMenuItem("打开对象文件成绩单");
        menuItem4 = new JMenuItem("另存为对象文件成绩单");
        menuItem5 = new JMenuItem("清除数据");
        menu.add(menuItem1);
        menu.add(menuItem2);
        menu.add(menuItem3);
        menu.add(menuItem4);
        menu.add(menuItem5);
        menuBar.add(menu);
        menuItem1.addActionListener(this);
        menuItem2.addActionListener(this);
        menuItem3.addActionListener(this);
        menuItem4.addActionListener(this);
        menuItem5.addActionListener(this);

        //搜索框
        searchPanel = new JPanel(new BorderLayout());
        jtf = new JTextField(10);
        searchPanel.add(jtf, BorderLayout.WEST);
        searchLabel = new JLabel("输入学号或者姓名或者成绩可以自动查询,输完中文打回车,点击列表头可以排序");
        searchLabel.setForeground(Color.red);
        searchPanel.add(searchLabel);
        searchPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
        jtf.addKeyListener(new JtfListener());

        setJMenuBar(menuBar);
        JPanel head = new JPanel();
        head.add(searchPanel);
        add(head, BorderLayout.NORTH);
    }

    private void normal() {
        setTitle("成绩分析程序");
        setLocation(200, 80);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String path = "image/bgp.jpg";
        background = new ImageIcon(getClass().getResource("/bgp.jpg"));
        JLabel label = new JLabel(background);// 把背景图片显示在一个标签里面
        // 把标签的大小位置设置为图片刚好填充整个面板
        label.setBounds(0, 0, background.getIconWidth(),
                background.getIconHeight());
        // 把内容窗格转化为JPanel，否则不能用方法setOpaque()来使内容窗格透明
        imagePanel = (JPanel) this.getContentPane();
        imagePanel.setOpaque(false);
        // 内容窗格默认的布局管理器为BorderLayout
        imagePanel.setLayout(new FlowLayout());

        // 把背景图片添加到分层窗格的最底层作为背景
        this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));

        this.setIconImage((new ImageIcon(getClass().getResource("/icon.jpg")).getImage()));
        this.setSize(900, 620);
        this.setResizable(false);
        this.setVisible(true);
    }

    /**
     * 内部类
     * 查询的按键监听类
     */
    class JtfListener implements KeyListener {

        public void keyTyped(KeyEvent e) {}

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                /**
                 * 查找信息
                 */
                table.setModel(MyTableModel.getTableModel(scorePanel.findDatas(jtf.getText())));
                table.repaint();
                table.updateUI();
            }
        }

        public void keyReleased(KeyEvent e) {}
    }
}
